package Group9.TodoManager.Actions;

import Group9.TodoManager.Settings;
import Group9.TodoManager.Data.Interface.ITableModel;
import Group9.TodoManager.Data.Task;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
 * This action is meant for setting the currently selected row index in the
 * table
 * @author Rupesh
 * @author volkan cambazoglu
 */
public class SelectAction<T> extends TodoManagerAction {

    private ITableModel<T> tableModel;

    /**
     * Constructor. Creates an instance of the SelectAction class.
     * @param table table model that is subject to selection
     */
    public SelectAction(ITableModel<T> table) {
        
    	tableModel = table;
        tableModel.addPropertyChangeListener(new PropertyChangeListener() {

        	/**
        	 * When property of the table model is changed, then excute here
        	 */
            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equalsIgnoreCase("SelectedRowIndex")) {
                	
                    int rowindex = Integer.parseInt(evt.getNewValue().toString());
                    
                    if (rowindex != -1) {
                        /**
                         * Present selected row in the south panel of the main UI
                         * The progress and content of the task will be shown
                         */
                        Task task = (Task) tableModel.getRowObject(rowindex);
                        Settings.getMainGUI().getTxtarea().setText(task.toString());
                        Settings.getMainGUI().setProgressModel(100, task.getProgress());
                    }
                    else {
                        //DO THE PART OF REMOVING THE TEXTAREA
                    	Settings.getMainGUI().resetTxtarea();
                    	Settings.getMainGUI().resetProgressModel();
                    }
                }
            }
        });
    }

    /**
     * When the action is executed, do what is necessary.
     * @param params
     */
    @Override
    public void executeAction(Object params) {
    	
    }
}